package com.group.project.hrms.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.group.project.hrms.entity.CheckPointInfo;
import com.group.project.hrms.entity.ResultEntity.ResultEntity;
import com.group.project.hrms.service.ICheckPointInfoService;
import com.group.project.hrms.utils.DateUtil;
import com.group.project.hrms.utils.LogUtil;
import com.group.project.hrms.utils.UUIDUtil;
import com.group.project.hrms.utils.VerificationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class CheckPointInfoController {

    @Autowired
    private ICheckPointInfoService iCheckPointInfoService;

    @Autowired
    private DateUtil dateUtil;

    @Autowired
    private VerificationUtil verificationUtil;

    @Autowired
    private LogUtil logUtil;

    /**
     * 该方法接收前端传来的创建打卡时段设置，并向后台数据库做插入操作
     * @param map 前端传来的数据集合
     * @param request http请求
     * @return 标准返回值对象
     */
    @RequestMapping("/basic/createCheckPointInfo")
    @ResponseBody
    public ResultEntity createCheckPointInfo(@RequestBody Map map, HttpServletRequest request){
        ResultEntity resultEntity = new ResultEntity();
        Object staffName = request.getAttribute("staffName");
        Date date = new Date();
        Object standard = map.get("standard");
        Object startToEnd = map.get("startToEnd");
        if(null != standard && null != startToEnd){
            try{
                Date standardTime = dateUtil.formatDateByString(standard.toString());
                JSONArray jsonArray = JSON.parseArray(JSON.toJSONString(startToEnd));
                Date startTime = dateUtil.formatDateByString(jsonArray.getString(0));
                Date endTime = dateUtil.formatDateByString(jsonArray.getString(1));
                CheckPointInfo checkPointInfo = new CheckPointInfo();
                checkPointInfo.setcTime(date);
                checkPointInfo.setCheckPointId(UUIDUtil.createUUID());
                checkPointInfo.setStandardTime(standardTime);
                checkPointInfo.setStartTime(startTime);
                checkPointInfo.setEndTime(endTime);
                int i = iCheckPointInfoService.insertCheckPointInfo(checkPointInfo);
                logUtil.insertSystemEventLog(staffName.toString(), date, "创建了一个打卡时间点" + standard, "考勤");
                if(i != 0){
                    resultEntity.setCode("S");
                    resultEntity.setMsg("创建成功");
                    return resultEntity;
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        resultEntity.setCode("F");
        resultEntity.setMsg("创建失败");
        return resultEntity;
    }

    /**
     * 该方法接收前端传来的参数，向数据库删除一个打卡时段
     * @param map 前端传入的参数
     * @param request http请求
     * @return 标准返回值对象
     */
    @RequestMapping("/basic/deleteCheckPointInfo")
    @ResponseBody
    public ResultEntity deleteCheckPointInfo(@RequestBody Map map, HttpServletRequest request){
        ResultEntity resultEntity = new ResultEntity();
        Object staffName = request.getAttribute("staffName");
        Object checkPointId = map.get("checkPointId");
        if(null != checkPointId){

            //需要先校验关联性再删除
            if(!verificationUtil.confirmDeleteCheckPoint(checkPointId.toString())){
                resultEntity.setCode("F");
                resultEntity.setMsg("删除失败，该打卡时段正在使用中");
                return resultEntity;
            }

            List<CheckPointInfo> list = iCheckPointInfoService.selectAllCheckPointInfo();
            int i = 0;
            for (CheckPointInfo c : list
                 ) {
                if(c.getCheckPointId().equals(checkPointId)){
                    i = iCheckPointInfoService.deleteCheckPointInfo(c);
                }
            }
            if(i != 0){
                resultEntity.setCode("S");
                resultEntity.setMsg("删除成功");
                logUtil.insertSystemEventLog(staffName.toString(), new Date(), "删除了一组打卡时间点", "考勤");
                return resultEntity;
            }
        }
        resultEntity.setCode("F");
        resultEntity.setMsg("删除失败");
        return resultEntity;
    }


}
